﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AutoShutdownDotNet.Actions;
using AutoShutdownDotNet.Models;

namespace AutoShutdownDotNet
{
    public partial class DoTaskForm : Form
    {
        #region Properties

        public Task Task { get; set; }

        public string Title
        {
            get { return lblTitle.Text; }
            set { this.Text = lblTitle.Text = value; }
        }

        public int TaskId { get; set; }

        private AutoShutdownAction _action;
        public AutoShutdownAction Action 
        { 
            get { return _action; } 
            set 
            { 
                _action = value;
                switch (value)
                {
                    case AutoShutdownAction.Hibernate:
                        lblDescription.Text = "Your Computer will be Hibernated in a few seconds.";
                        break;
                    case AutoShutdownAction.Lock:
                        lblDescription.Text = "Your Computer will be Locked in a few seconds.";
                        break;
                    case AutoShutdownAction.Logoff:
                        lblDescription.Text = "You will be logged off in a few seconds.";
                        break;
                    case AutoShutdownAction.PowerOff:
                        lblDescription.Text = "Your Computer will be Shutdown in a few seconds.";
                        break;
                    case AutoShutdownAction.Reboot:
                        lblDescription.Text = "Your Computer will be hibernate in a few seconds.";
                        break;
                    case AutoShutdownAction.RunApplication:
                        lblDescription.Text = "An application will be executed in a few seconds." + Environment.NewLine +
                                              "Path: " + FilePath;
                        break;
                    case AutoShutdownAction.Standby:
                        lblDescription.Text = "Your Computer will be put to Sleep in a few seconds.";
                        break;
                }
            } 
        }

        public string FilePath { get; set; }

        public int _timer = 0;
        public int Timer
        {
            get { return _timer; }
            set { _timer = value;
                timer.Enabled = true;
                lblTimer.Text = value.ToString() + " second(s) to decide.";
                if(value == 0)
                {
                    DoNow();
                }
                if (value < 10)
                    lblTimer.ForeColor = Color.Red;
            }
        }

        #endregion


        public DoTaskForm(Task task)
        {
            InitializeComponent();
            Task = task;
            Title = task.Title;
            Action = task.Action;
            FilePath = Task.FilePath;
            Timer = task.Timer;
            TaskId = task.Id;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            Timer--;
        }

        private void DoNow()
        {
            DoAction doAction = new DoAction(Action);
            if (Action == AutoShutdownAction.RunApplication)
                doAction.FilePath = FilePath;
            doAction.Do();
            Close();
        }

        private void btnDoItNow_Click(object sender, EventArgs e)
        {
            DoNow();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Hide();
        }
    }
}
